package com.h2.ref.server.user.relationship;

import java.util.ArrayList;
import java.util.Collection;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONException;
import org.springframework.context.annotation.Scope;

import com.h2.ref.server.user.model.User;
import com.h2.ref.server.user.model.UserLod;
import com.h2.ref.server.user.relationship.UserRelationship.UserRelationshipType;
import com.sun.jersey.api.json.JSONWithPadding;

@Path("/relationship")
@Scope("singleton")
public class UserRelationshipServiceJson {
   
   private IUserRelationshipService _userRelationshipService;
   
   ////
   ////
   
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/find")
   public JSONWithPadding find(
         @QueryParam("actorId")
         Integer actorId,
         @QueryParam("targetId")
         Integer targetId,
         @QueryParam("type")
         String type) throws JSONException {
      
      UserRelationship relationship = new UserRelationship();
      relationship.setActorId(actorId);
      relationship.setTargetId(targetId);
      if (type != null) {
         relationship.setRelationshipType(UserRelationshipType.valueOf(type));
      }
      
      return new JSONWithPadding(
            getUserRelationshipService().find(relationship));
   }
   
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/findUsers")
   public JSONWithPadding findUsers(
         @QueryParam("actorId")
         Integer actorId,
         @QueryParam("targetId")
         Integer targetId,
         @QueryParam("type")
         String type) throws JSONException {
      
      UserRelationship relationship = new UserRelationship();
      relationship.setActorId(actorId);
      relationship.setTargetId(targetId);
      if (type != null) {
         relationship.setRelationshipType(UserRelationshipType.valueOf(type));
      }
      
      Collection<User> users =
            getUserRelationshipService().findUsers(relationship);
      Collection<UserLod> results = new ArrayList<UserLod>();
      for (User user : users) {
         results.add(convert(user));
      }
      
      return new JSONWithPadding(results);
   }

   @GET
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/create")
   public JSONWithPadding create(
         @QueryParam("actorId")
         Integer actorId,
         @QueryParam("targetId")
         Integer targetId,
         @QueryParam("type")
         String type) throws JSONException {
      
      UserRelationship relationship = new UserRelationship();
      relationship.setActorId(actorId);
      relationship.setTargetId(targetId);
      relationship.setRelationshipType(UserRelationshipType.valueOf(type));
      
      return new JSONWithPadding(
            getUserRelationshipService().create(relationship));
   }
   
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/delete")
   public JSONWithPadding delete(
         @QueryParam("actorId")
         Integer actorId,
         @QueryParam("targetId")
         Integer targetId,
         @QueryParam("type")
         String type) throws JSONException {
      
      UserRelationship relationship = new UserRelationship();
      relationship.setActorId(actorId);
      relationship.setTargetId(targetId);
      if (type != null) {
         relationship.setRelationshipType(UserRelationshipType.valueOf(type));
      }
      
      return new JSONWithPadding(
            getUserRelationshipService().delete(relationship));
   }
   
   private UserLod convert(User user) {
      UserLod userLod = new UserLod();
      userLod.setId(user.getId());
      userLod.setName(user.getName());
      
      return userLod;
   }

   /**
    * @return the userRelationshipService
    */
   public IUserRelationshipService getUserRelationshipService() {
      return _userRelationshipService;
   }

   /**
    * @param userRelationshipService the userRelationshipService to set
    */
   public void setUserRelationshipService(
         IUserRelationshipService userRelationshipService) {
      _userRelationshipService = userRelationshipService;
   }
}
